using AvalonAdmin.BaseViewModel;
using AvalonAdminDto.Common;
using AvalonAdminDto.Rule.RuleConfigDtos;
using System.Threading.Tasks;

namespace AvalonAdmin.Pages.Rule.ViewModels.RuleConfig
{
    /// <summary>
    /// 规则配置详情修改视图模型
    /// </summary>
    public class RuleConfigDetailsModifyViewModel : BaseModifyViewModel
    {
        #region 构造函数
        private readonly RuleConfigDetails _ruleConfigDetails;
        
        /// <summary>
        /// 初始化规则配置详情修改视图模型
        /// </summary>
        /// <param name="ruleConfigDetails">规则配置详情</param>
        public RuleConfigDetailsModifyViewModel(RuleConfigDetails ruleConfigDetails) : base()
        {
            _ruleConfigDetails = ruleConfigDetails;
            LoadInitialData();
        }
        #endregion

        #region 属性
        private string? _position;
        /// <summary>
        /// 位置
        /// </summary>
        public string? Position
        {
            get => _position;
            set => SetProperty(ref _position, value);
        }

        private string? _value;
        /// <summary>
        /// 值
        /// </summary>
        public string? Value
        {
            get => _value;
            set => SetProperty(ref _value, value);
        }
        #endregion

        #region 事件
        /// <summary>
        /// 信息请求事件委托
        /// </summary>
        public delegate void InformationRequestedHandler(object sender, InformationEventArgs<RuleConfigDetails> e);
        
        /// <summary>
        /// 信息请求事件
        /// </summary>
        public event InformationRequestedHandler? InformationRequested;
        #endregion

        #region 私有方法
        /// <summary>
        /// 加载初始数据
        /// </summary>
        private void LoadInitialData()
        {
            if (_ruleConfigDetails != null)
            {
                Position = _ruleConfigDetails.Position;
                Value = _ruleConfigDetails.Value;
            }
        }
        #endregion

        #region 重写基类方法
        /// <summary>
        /// 重置表单
        /// </summary>
        protected override void ResetForm()
        {
            Position = string.Empty;
            Value = string.Empty;
        }

        /// <summary>
        /// 异步提交数据
        /// </summary>
        protected override async Task SubmitAsync()
        {
            var ruleConfigDetails = new RuleConfigDetails
            {
                Position = Position,
                Value = Value
            };

            InformationRequested?.Invoke(this, new InformationEventArgs<RuleConfigDetails>(ruleConfigDetails));
            await Task.CompletedTask;
        }
        #endregion
 
    }
}
